package com.zut.controller;

import com.zut.pojo.Parents;
import com.zut.pojo.Product;
import com.zut.service.ProductService;
import com.zut.service.impl.ProductServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ProductController {
    @Resource
    private ProductServiceImpl productService;

    //添加课程包
    @GetMapping("/addpro")
    public  String addlpro(){return  "back/add-subject";}

    @PostMapping("/addpro")
    public String addPro(Product product, Model model){
        boolean i = productService.addPro(product);
        if(i==false){
            model.addAttribute("msg","添加失败");
        }else{
            model.addAttribute("msg","添加成功");
        }
        return "back/add-subject";
    }

    //购买课程包
    @RequestMapping("/buyPro")
    public void buyPro(Integer parid,Integer proid,Model model){

    }

    //修改课程包
    @GetMapping("/updatepro/{id}")
    public String updatePro(@PathVariable("id") Integer id, Model model){
        Product product = productService.selectProductById(id);
        model.addAttribute("pro",product);
        return "back/edit-subject";
    }
    @PostMapping("/updatepro/{id}")
    public String updateProduct(@PathVariable("id") Integer id,  Product product,Model model){
        boolean flag=productService.updatePro(product);
        if(flag!=false){
            model.addAttribute("msg","修改成功");
        }else{
            model.addAttribute("msg","修改失败");
        }
        return "redirect:/updatepro/{id}";
    }

    //删除课程包
    @GetMapping("/delpro/{id}")
    public String delpro(@PathVariable("id") Integer id){
        productService.deletePro(id);
        return "redirect:/admpro";
    }


    //删除课时包
    @RequestMapping("/deletePro")
    public void deletePro(Integer proid,Model model){
        int i = productService.deletePro(proid);
        if(i==1){
            model.addAttribute("msg","删除成功");
        }else{
            model.addAttribute("msg","删除失败");
        }
    }

    //课时包展示
    @GetMapping("/shop")
    public String findById(Model model){
        List<Product> list = productService.list();
        model.addAttribute("products",list);
        return "shop";
    }

    //课时包购买
    @GetMapping("/shop/{proid}")
    public String findById(@PathVariable("proid") Integer proid, Model model, HttpSession session){
        Parents parent = (Parents) session.getAttribute("loginParent");
        int parid=parent.getParid();
        boolean buyPro = productService.buyPro(parid, proid);
        if(buyPro){
            model.addAttribute("msg","购买成功");
        }else{
            model.addAttribute("msg","购买失败");
        }
        return "redirect:/shop";
    }
}
